﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jy123.BLL.User;
using Jy123.Common;

namespace Jy123.Web.home
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class register : IHttpHandler
    {
        
        protected string province = string.Empty;
        protected string cityarray = string.Empty;
        protected int succeed = 0;
        protected string error = string.Empty;
        protected string email = string.Empty;
        protected string sex = string.Empty;
        protected string isRegMobile = string.Empty;
        public void ProcessRequest(HttpContext context)
        {
            
            string email = context.Request["txtEmail"];
            bool emalIsAvailable = new BLL.User.UserSql().CheckEmailAvailability(email);
            string cdata = context.Request["aaa"];
            string reg = context.Request["Register"];
            if (reg == "Submit")//当提交注册请求
            {
                Reg(context);//进行注册
            }
        }
        /// <summary>
        /// 检查email是否已存在
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool CheckEmailAvailability(string email)
        {
            Jy123.BLL.User.UserSql bllUserSql = new Jy123.BLL.User.UserSql();
            bool emailIsAvailable = bllUserSql.CheckEmailAvailability(email);
            if (emailIsAvailable) { return true; }
            return false;
        }
        //进行注册
        private void Reg(HttpContext context)
        {
            
            Model.User_Main modelUser = new Jy123.Model.User_Main();//用户表实体

            string InviteCode = context.Request["InviteCode"].Trim();
            bool blHasInviteCode = false;
            if (!string.IsNullOrEmpty(InviteCode) && InviteCode.Contains("InviteCode="))
            {
                blHasInviteCode = true;
                InviteCode = InviteCode.Substring(InviteCode.IndexOf("InviteCode=") + 11);
                modelUser.InviterID = new Jy123.BLL.User.UserSql().GetUserIDByVeryfyCode(DBUtility.DESEncrypt.Decrypt(InviteCode));
            }
            
            email = context.Request["txtEmail"];
            modelUser.Email = email;
            modelUser.UserName = context.Request["txtName"];
            modelUser.Password = context.Request["txtPsw"];
            modelUser.isRec = 0;
            modelUser.LastLoginIP = Common.Public.GetClientIP();
            modelUser.LastLoginTime = DateTime.Now;
            
            bool notFull = false;//是否填满信息
            if (string.IsNullOrEmpty(context.Request["txtEmail"])
                || string.IsNullOrEmpty(context.Request["txtName"])
                || string.IsNullOrEmpty(context.Request["txtPsw"])
                || string.IsNullOrEmpty(context.Request["txtPsw2"])
                //|| string.IsNullOrEmpty(context.Request.Form["hidCity"])
                //|| string.IsNullOrEmpty(context.Request["hidBirthday"])
                || string.IsNullOrEmpty(context.Request["job"]))
            {
                notFull = true;//有信息未填写
            }

            Model.User_Info modelUserBasicInfo = new Jy123.Model.User_Info();//用户信息表实体

            string strSex = context.Request["sex"];
            int sx = Convert.ToInt32(strSex);
            modelUserBasicInfo.Sex = Convert.ToBoolean(sx);
            //modelUserBasicInfo.Birthday = DateTime.Parse(context.Request["hidBirthday"]);

            string strJob = context.Request["job"];
            modelUserBasicInfo.Vocation = int.Parse(strJob);
            //非空检查
            if (string.IsNullOrEmpty(context.Request["sex"])) { notFull = true; }
            if (notFull) { error = "所有项目必须填写！"; }

            //检查两次密码
            bool chkPsw = Equals(context.Request["txtPsw"], context.Request["txtPsw2"]);
            //隐私设置
            string selPrivacy = context.Request["selPrivacy"];
            
            EnumPrivacy privacy = (EnumPrivacy)Convert.ToInt32(selPrivacy);

            //插入数据库
            if (!notFull)
            {
                if (chkPsw)
                {
                    Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
                    int ID = 0;
                    EnumRegister eReg = new Jy123.BLL.User.UserSql().Register(modelUser, modelUserBasicInfo,privacy,out ID);//将注册记录插入数据库
                    if (eReg == EnumRegister.Succeed)
                    {
                        succeed = 1;
                        Cookie.SetCookie("UserName", modelUser.UserName);
                        Cookie.SetCookie("UserID", ID.ToString());
                        Cookie.SetCookie("UserEmail", email);
                    }
                    try
                    {
                        if (succeed == 1)
                        {
                            if (blHasInviteCode)
                            {
                                modelUser = bllUser.GetModel(Convert.ToInt32(modelUser.InviterID));
                                int addMoney =  Convert.ToInt32(Common.Config.BaseConfig.GetConfigValue("AddInviterMoney"));
                                modelUser.Cash +=addMoney; //给邀请者加钱;
                                bllUser.Update(modelUser);
                                Jy123.Model.User_Friend mUserFrd = new Jy123.Model.User_Friend();
                                Jy123.BLL.User_Friend bllUserFrd = new Jy123.BLL.User_Friend();


                                //发好友申请给邀请者
                                mUserFrd.FUser = ID;
                                mUserFrd.FFriend = modelUser.ID;
                                mUserFrd.State = 0;
                                mUserFrd.AddTime = DateTime.Now;
                                mUserFrd.FFriendClass = 0;//默认好友分组
                                bllUserFrd.Add(mUserFrd);

                                //加入动态
                                Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                                Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                                mUserForState = bllUserForState.GetModel(ID);

                                new Jy123.BLL.Public.State().CreateInviteFriendState(modelUser,mUserForState,addMoney);
                                //加入动态
                            }                  
                            
                        }
                    }
                    catch
                    { 
                    }
                }
                else
                {
                    context.Response.Write("<script>$(function(){$.moonbox.error('两次密码不一致');})</script>");
                }
            }
            else
            {
                context.Response.Write("<script>$(function(){$.moonbox.error('失败');})</script>");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
